package com.bootdo.common.service.impl;

import com.bootdo.common.config.BootdoConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.bootdo.common.dao.FileDao;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.service.FileService;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;


@Service
public class FileServiceImpl implements FileService {
	@Autowired
	private FileDao sysFileMapper;

	@Autowired
	private BootdoConfig bootdoConfig;
	@Override
	public FileDO get(Long id){
		return sysFileMapper.get(id);
	}
	
	@Override
	public List<FileDO> list(Map<String, Object> map){
		return sysFileMapper.list(map);
	}

	@Override
	public List<FileDO> getByFileOwner(String fileOwner) {
		return sysFileMapper.getByFileOwner(fileOwner);
	}

	@Override
	public FileDO getFile(Map<String, Object> map) {
		return sysFileMapper.getFile(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return sysFileMapper.count(map);
	}
	
	@Override
	public int save(FileDO sysFile){
		return sysFileMapper.save(sysFile);
	}
	
	@Override
	public int update(FileDO sysFile){
		return sysFileMapper.update(sysFile);
	}
	
	@Override
	public int remove(Long id){
		return sysFileMapper.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return sysFileMapper.batchRemove(ids);
	}

	@Override
	public int batchRemoveByFileOwner(String fileOwner) {
		return sysFileMapper.batchRemoveByFileOwner(fileOwner);
	}

	@Override
	public int removeFile(Map<String, Object> map) {
		return sysFileMapper.removeFile(map);
	}

	@Override
	public Boolean isExist(String fileName, String fileOwner) {
			Boolean isExist = false;
			if (!StringUtils.isEmpty(fileName)) {
				String filePath = fileOwner+"/"+fileName; //fileName.replace("/"+fileOwner+"/", "");
				filePath = bootdoConfig.getUploadPath() + filePath;
				File file = new File(filePath);
				if (file.exists()) {
					isExist = true;
				}
			}
			return isExist;
		}

	}
